<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <h2>公司组织结构 :</h2>
        <treeTools :tree-node="company" :is-root="true" @addDepts="addDepts" @editDets="editDets" />
        <hr>
        <el-tree default-expand-all :data="departs" :indent="50">
          <!-- slot-scope 是 tree组件传给每个节点的插槽的内容的数据 -->
          <treeTools
            slot-scope="{ data }"
            :tree-node="data"
            @addDepts="addDepts"
            @delDepts="toGetDepartments"
            @editDepts="editDepts"
          /><!-- 删除后刷新数据 -->
        </el-tree>
      </el-card>
      <!-- 弹窗 -->
      <!-- <addDept :show-dialog="showDialog" :node="node" @addDepts="getDepartments" @delDepts="showDialog=false" /> -->
      <!-- 使用sync 修饰符 -->
      <addDept
        ref="addDept"
        :show-dialog.sync="showDialog"
        :node="node"
        @addDepts="toGetDepartments"
      />
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools'
import addDept from './components/add-dept'
import { getDepartmentList } from '@/api/departments'
import { transListToTreeData } from '@/utils/index'

export default {
  components: { treeTools, addDept },
  data() {
    return {
      // 树形结构数据
      departs: [],
      company: { name: '广东泽基网络科技有限公司', manager: '负责人', id: '' },
      // 输入框显示和输入值对象
      showDialog: false,
      node: {}
    }
  },
  async created() {
    this.toGetDepartments()
  },
  methods: {
    // 封装获取组织数据
    async toGetDepartments() {
      const res = await getDepartmentList()
      this.departs = transListToTreeData(res.depts, '')
    },
    // 添加部门
    addDepts(treeNode) {
      // 弹出输入框
      this.showDialog = true
      // 传递数据
      this.node = treeNode
    },
    // 编辑部门
    editDets(treeNode) {
      this.node = treeNode
    },
    // 编辑
    editDepts(treeNode) {
      // 进行数据回传给
      this.$refs.addDept.toGtDepartment(treeNode.id)
      // 弹出输入框
      this.showDialog = true
      // 传递数据
      this.node = treeNode
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 10px 250px 30px;
  font-size: 16px;
}
</style>
